﻿using Futajma.MigrationStudio;
using Futajma.MigrationStudio.GenericPersistance;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LLTreeManagementApplication.Dialog
{
    public partial class ActionItemManagement : Form
    {
        ActionItemHandler _actionItemHandler;


        public ActionItemManagement(ActionItemHandler actionItemHandler)
        {
            _actionItemHandler = actionItemHandler;
            InitializeComponent();

            InitForm();
        }

        private void InitForm()
        {
            dataGridView.CellFormatting += dataGridView_CellFormatting;
            dataGridView.AutoGenerateColumns = false;
            dataGridView.DataSource = _actionItemHandler.ActionList;
        }

        void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataGridView.Columns[e.ColumnIndex].Name == "Status")
            {
                var action = _actionItemHandler.ActionList[e.RowIndex];

                if (action.Completed == State.False)
                {
                    e.Value = Resource.Error_red_16x16;
                }
                else if (action.Completed == State.True)
                {
                    e.Value = Resource.StatusAnnotations_Complete_and_ok_16xLG_color;
                }
                else
                {
                    e.Value = Resource.StatusAnnotations_Help_and_inconclusive_16xLG_color;
                }
            }
        }

        private void btnRejectStep_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                _actionItemHandler.ActionList[dataGridView.SelectedRows[0].Index].Completed = State.False;

                dataGridView.SelectedRows[0].Cells["Status"].Value = Resource.Error_red_16x16;
            }

        }

        private void btnAcceptStep_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                _actionItemHandler.ActionList[dataGridView.SelectedRows[0].Index].Completed = State.True;

                dataGridView.SelectedRows[0].Cells["Status"].Value = Resource.StatusAnnotations_Complete_and_ok_16xLG_color;

                if (dataGridView.Rows.Count > dataGridView.SelectedRows[0].Index + 1)
                {
                    dataGridView.Rows[dataGridView.SelectedRows[0].Index + 1].Selected = true;
                }
            }
        }

        private void btnDetailInfos_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                _actionItemHandler.ActionList[dataGridView.SelectedRows[0].Index].Completed = State.Issue;

                dataGridView.SelectedRows[0].Cells["Status"].Value = Resource.StatusAnnotations_Help_and_inconclusive_16xLG_color;

                if (dataGridView.Rows.Count > dataGridView.SelectedRows[0].Index + 1)
                {
                    dataGridView.Rows[dataGridView.SelectedRows[0].Index + 1].Selected = true;
                }
            }
        }

        private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            int i = 0;
            foreach (var action in _actionItemHandler.ActionList)
            {
                if (action.Completed != State.True)
                {
                    dataGridView.Rows[i].Selected = true;
                    break;
                }
                i++;
            }
        }


    }
}
